<?php
!defined ('P_W') && exit('Forbidden');
require_once R_P.'mode/house/lib/db/BaseDB.php';
class PW_HousemapDB extends PW_BaseDB{
	var $_tablename = "pw_house_map";
	var $_search = "";
	
	function __construct() {
		global $house_mapsearch;
		$this->_search = & $house_mapsearch;
		parent::__construct();
	}
	
	/**
	 * 根据楼盘ID搜索周边楼盘
	 * @param $hid
	 * @return unknown_type
	 */
	function searchHouseMapByPosition($hid,$num = 15){
		$result = array();
		if ($hid) {
			$point = $this->queryHouseMapByHid($hid);
			if (is_array($point) && !empty($point)) {
				list($_x_max,$_x_min,$_y_max,$_y_min) = $this->_getEdgePointsPositions($point['x'],$point['y']); 
				$_limit = "";
				( isset($num) && is_numeric($num) ) && $_limit = " LIMIT 0,$num";
				$_sql = "SELECT m.*,h.name,h.address,h.area,h.saletele,h.saleaddress,h.aveprice,ex.propertycompany FROM ".$this->_tablename." m LEFT JOIN pw_house_info h ON m.hid = h.hid LEFT JOIN pw_house_infoextra ex ON m.hid = ex.hid WHERE m.x > ".pwEscape($_x_min)." AND 
						 m.x < ".pwEscape($_x_max)." AND m.y > ".pwEscape($_y_min)." AND m.y < ".pwEscape($_y_max)." AND h.hid<>$hid $_limit ";
				$query = $this->_db->query($_sql);
				while ($rt = $this->_db->fetch_array($query)) { 
					$result[] = $rt;
				}
			}
		}
		return $result;
	}
	
	/**
	 * 根据边缘坐标点，确定搜索范围
	 * @param $point_x	X轴坐标
	 * @param $point_y	Y轴坐标
	 * @return array
	 */
	function _getEdgePointsPositions($point_x,$point_y){
		$_q = 111.12;
		$_x = $_y= 0;
		$_x = (int)$this->_search / $_q;
		$_y = (int)$this->_search / ($_q * cos($point_y));
		$_x_max = $point_x + $_x;
		$_x_min = $point_x - $_x;
		$_y_max = $point_y + $_y;
		$_y_min = $point_y - $_y;
		return array($_x_max,$_x_min,$_y_max,$_y_min);
	}
	
	/**
	 * @param $hids
	 * @return unknown_type
	 */
	function queryHouseMapByHids($hids,$order = 0){
		$result = array();
		if (is_array($hids) && !empty($hids)) {
			$order_sql = "";
			if ($order == 0) {
				$order_sql = " ORDER BY m.hid DESC";
			} elseif ($order) {
				$order_sql = " ORDER BY m.hid ASC";
			}
			$_sql = "SELECT h.hid,m.x,m.y,m.zoom,m.laber,h.name,h.address,h.saletele,h.saleaddress,h.aveprice,ex.propertycompany FROM " . $this->_tablename . " m RIGHT JOIN pw_house_info h ON m.hid = h.hid RIGHT JOIN pw_house_infoextra ex ON h.hid = ex.hid WHERE h.hid IN (".pwImplode($hids).") ".$order_sql;
			//$_sql = "SELECT m.*, h.name, h.address,h.saletele,h.saleaddress,h.aveprice,ex.propertycompany FROM ".$this->_tablename." m LEFT JOIN pw_house_info h ON m.hid = h.hid LEFT JOIN pw_house_infoextra ex ON m.hid = ex.hid WHERE m.hid IN (".pwImplode($hids).")".$order_sql;
			$query = $this->_db->query($_sql);
			while ($rt = $this->_db->fetch_array($query)) {
				$result[] = $rt;
			}
		}
		return $result;
	}
	
	/**
	 * 根据楼盘ID查询楼盘地图信息
	 * @param $hid
	 * @return unknown_type
	 */
	function queryHouseMapByHid($hid){
		$result = array();
		if ($hid) {
			$_sql = "SELECT m.*, h.name FROM ".$this->_tablename." m LEFT JOIN pw_house_info h ON m.hid = h.hid WHERE m.hid=".pwEscape($hid);
			$result = $this->_db->get_one($_sql);
		}
		return $result;
	}
	
	/**
	 * 添加一条楼盘数据
	 * @param $data
	 * @return unknown_type
	 */
	function insertHouseMap($data){
		if (is_array($data) || !empty($data)) {
			$_sql = "INSERT INTO ".$this->_tablename." SET ".pwSqlSingle($data);
			$this->_db->update($_sql);
		}
	}
	
	/**
	 * 删除楼盘地图信息根据楼盘ID
	 * @param $hid
	 * @return unknown_type
	 */
	function deleteHouseMapByHid($hid){
		if ($hid) {
			$_sql = "DELETE FROM ".$this->_tablename." WHERE hid=".pwEscape($hid);
			$this->_db->update($_sql);
		}
	}
	
	/**
	 * 删除楼盘地图信息根据ID
	 * @param $id
	 * @return unknown_type
	 */
	function deleteHouseMapById($id){
		if ($id) {
			$_sql = "DELETE FROM ".$this->_tablename." WHERE id=".pwEscape($id);
			$this->_db->update($_sql);
		}
	}
	
	/**
	 * 更新地图数据
	 * @param $data
	 * @return unknown_type
	 */
	function replaceHouseMapById($data){
		if (is_array($data) && !empty($data)) {
			$_sql = "REPLACE INTO ".$this->_tablename." SET ".pwSqlSingle($data);
			$this->_db->update($_sql);
		}
	}
		
}
?>